#ifndef FASYS_FASYS_SSERVICE_PUBLISH_MEHTOD_HPP
#define FASYS_FASYS_SSERVICE_PUBLISH_MEHTOD_HPP

#include <fas/adv/json_rpc3/method.hpp>

#include "../api/publish_json.hpp"
#include "tags.hpp"

namespace fasys{ namespace sservice{

namespace aa = ::fas::aop;
namespace ap = ::fas::pattern;
namespace ad = ::fas::adv;
namespace ajr = ::fas::adv::json_rpc3;
namespace aj = ::fas::json;

struct publish_method
{
  const char *name() const { return "publish"; }

  typedef fasys::api::publish_json invoke_notify;
  typedef fasys::api::publish_json invoke_request;
  typedef aj::value<bool> invoke_response;

  /*typedef publish_json call_notify;
  typedef message_json call_request;
  typedef message_json call_response;
  */

  typedef fasys::api::publish_json::target request_type;
  typedef fasys::api::publish_json::target response_type;

  template<typename T>
  void clear(T&) { }

  // ipublisher* publisher;

  template<typename T>
  bool request(T& t, request_type& cmd, int id, bool& result )
  {
    std::cout << "yo!! sservice: PUBLISH METHOD " << id << std::endl;
    t.publisher->publish(cmd);
    result = true;
    return true;
  }

  template<typename T>
  void notify(T& t, request_type& cmd )
  {
    std::cout << "yo!! sservice: PUBLISH METHOD" << std::endl;
    t.publisher->publish(cmd);
    t.get_aspect().template get<ajr::_writer_>()(t, (const char*)(0), size_t(0) );
  }
};

typedef aa::tag_list_n<
  aa::tag<_publish_>,
  aa::gtag< ajr::_gmethod_ >,
  aa::gtag< ad::_cleaner_ >
>::type publish_method_tag_list;

struct publish_method_advice
  : aa::advice<
      publish_method_tag_list,
      ajr::method<publish_method>
    >
{};

}}

#endif
